/* 
 * File:   Lesson5.cpp
 * Author: jose
 * 
 * Created on March 29, 2012, 2:17 PM
 */

#include "Lesson5.h"
#include "SdlUtils.h"
#include <iostream>

using namespace std;

Lesson5::Lesson5() {
}

Lesson5::Lesson5(const Lesson5& orig) {
}

Lesson5::~Lesson5() {
}

bool Lesson5::init(){
        //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }

    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

    //If there was an error in setting up the screen
    if( screen == NULL )
    {
        return false;
    }

    //Set the window caption
    SDL_WM_SetCaption( "Color Key", NULL );

    //If everything initialized fine
    return true;
}

int Lesson5::Run(){
    
    if( init() == false)
    {
        cout << "Could not init" << endl;
        return 1;
    }
    
    background = SdlUtils::loadImageColorKey("background.png");
    
    if(background == NULL)
    {
        cout << "Could not load the bg image" << endl;
        return 1;
    }
    message = SdlUtils::loadImageColorKey("foo.png");
    
    if(message == NULL)
    {
        cout << "Could not load the image" << endl;
        return 1;
    }
    
    SdlUtils::applySurface(0, 0, background, screen );
    SdlUtils::applySurface(240, 190, message, screen );
    
    if(SDL_Flip(screen) == -1)
    {
        return 1;
    }
    
    bool quit = false;
    SDL_Event event;
    
    while(!quit)
    {
        while(SDL_PollEvent(&event))
        {
            if(event.type == SDL_QUIT)
            {
                quit = true;
            }
        }
    }
    
    SDL_FreeSurface( message );
    SDL_FreeSurface( background );

    SDL_Quit();
    
    return 0;
}
